﻿using System.Collections.Generic;
using System.Data;
using IBatisNet.DataMapper;

namespace Wolfut.MirageEE.Core.DAO
{
    public class BaseADODao : BaseDao
    {
        //protected Database GetLocalDatabase()
        //{
        //    var session = (SqlMapSession) GetContext();
        //    return DatabaseFactory.CreateDatabase(session.DataSource.ConnectionString,
        //                                          ConvertDbType(session.DataSource.DbProvider.Name));
        //}

        //private DataBaseType ConvertDbType(string providerName)
        //{
        //    if (providerName.ToLower().IndexOf("sqlserver") >= 0)
        //    {
        //        return DataBaseType.SqlServer;
        //    }
        //    else if (providerName.ToLower().IndexOf("oracleClient") >= 0)
        //    {
        //        return DataBaseType.OracleClient;
        //    }
        //    return DataBaseType.SqlServer;
        //}

        public IDbDataAdapter dbDataAdapter()
        {
            return GetContext().CreateDataAdapter();
        }

        public IDbDataAdapter dbDataAdapter(IDbCommand dbCommand)
        {
            return GetContext().CreateDataAdapter(dbCommand);
        }

        public IDbDataParameter CreateDbDataPrameter()
        {
            return GetContext().CreateDataParameter();
        }

        public IDbCommand getDbCommand()
        {
            return getDbCommand(CommandType.Text);
        }

        public IDbCommand getDbCommand(CommandType type)
        {
            return GetContext().CreateCommand(type);
        }

        /// <summary>
        /// 根据SQL和参数返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(string sql, IList<IDbDataParameter> sqlParams)
        {
            IDbCommand cmd = getDbCommand();
            try
            {
                cmd.CommandText = sql;
                if (sqlParams != null)
                {
                    for (int i = 0; i < sqlParams.Count; i++)
                        cmd.Parameters.Add(sqlParams[i]);
                }
                var ds = new DataSet();
                dbDataAdapter(cmd).Fill(ds);
                return ds;
            }
            finally
            {
                cmd.Dispose();
            }
        }
    }
}